﻿using DataWorks_Tools.BasicMethods;
using DataWorks_Tools.MappModals;
using DataWorks_Tools.MileageCalDetails;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataWorks_Tools.ProcessExcute.MileageCal
{
    public class MileageCalProcess
    {
        /// <summary>
        /// 计算某一路径下每个日期文件夹下的所有csv数据的总里程数和odo的对比，根据用户输入的日期范围来决定计算哪个文件夹
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        [Obsolete]
        public static async Task<List<MileageCalculationResultModel>> MileageCalProcessExcute(MileageCalculationModel row)
        {
            var directories = MileageCalculation.GetDirectoryArrayfromDate(row);
            List<MileageCalculationResultModel> result = new List<MileageCalculationResultModel>();
            if (directories.Length > 0)
            {
                foreach (var sub in directories)
                {
                    MileageCalculationResultModel data = new MileageCalculationResultModel();
                    var dirname = new DirectoryInfo(sub).Name;
                    var csvfiles = sub.GetCSVFiles();
                    if (csvfiles.Length > 0)
                    {
                        double firstvalue = 0;
                        double lastvalue = 0;
                        double caldis = 0;
                        var first = CSVHelperClass.GetDataFromColumnName(csvfiles.First(), row.odochanname);
                        if (first.Count > 0)
                        {
                            firstvalue = first[0];
                        }
                        var last =  CSVHelperClass.GetDataFromColumnName(csvfiles.Last(), row.odochanname);
                        if (last.Count > 0)
                        {
                            lastvalue = last[last.Count - 1];
                        }
                        data.odo = Math.Round(lastvalue - firstvalue, 2);
                        foreach (var csvfile in csvfiles)
                        {
                            var spd = CSVHelperClass.GetDataFromColumnName(csvfile, row.spdchanname);
                            var time = CSVHelperClass.GetDataFromColumnIdx(csvfile, 0);
                            if (spd.Any(a => a == -1.0) || time.Any(a => a == -1.0))
                            {
                                data.info = $"{csvfile.Name}的time数据或者{row.spdchanname}数据有错误";
                                //data.calmileage = -1.0;
                                break;
                            }
                            else
                            {
                                var dis = await MileageCalculation.ReturnSingleDistance(spd, time);
                                caldis += dis.Sum();
                            }
                        }
                        data.info = $"{dirname}计算正常";
                        data.calmileage = Math.Round(caldis / 1000, 2);
                        data.difference = Math.Round(data.calmileage - data.odo, 1);
                    }
                    else
                    {
                        data.info = $"{dirname}下没有数据{Environment.NewLine}";
                    }
                    result.Add(data);
                }
            }
            else
            {
                MileageCalculationResultModel data = new MileageCalculationResultModel();
                var noinfo = $"你选的日期路径没有数据{Environment.NewLine}";
                result.Add(data);
            }
            return result;
        }

        /// <summary>
        /// 计算某一路径下每个日期文件夹下的所有csv数据的总里程数和odo的对比，此方法会跳过有问题的csv文件
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public static async Task<List<MileageCalculationResultModel>> MileageCalProcessExcuteV2(MileageCalculationModel row)
        {
            var directories = MileageCalculation.GetDirectoryArrayfromDate(row);
            List<MileageCalculationResultModel> result = new List<MileageCalculationResultModel>();
            if (directories.Length > 0)
            {
                foreach (var sub in directories)
                {
                    MileageCalculationResultModel data = new MileageCalculationResultModel();
                    var dirname = new DirectoryInfo(sub).Name;
                    var csvfiles = sub.GetCSVFiles();
                    if (csvfiles.Length > 0)
                    {
                        var odo = await MileageCalculation.GetTotalMileagesfromODO(csvfiles, row.odochanname);
                        if (odo == -1)
                        {
                            data.info= $"{dirname}下所有数据的odo都有问题{Environment.NewLine}";
                            return result;
                        }
                        else if (odo == -2)
                        {
                            data.info = $"{dirname}下数据为空{Environment.NewLine}";
                            return result;
                        }
                        data.odo = odo;
                        double caldis = 0;
                        foreach (var csvfile in csvfiles)
                        {
                            var spd =await MileageCalculation.CSVFileSelectChantitleAsync(csvfile.FullName, row.spdchanname);
                            var time = await MileageCalculation.CSVFileSelectChanidxAsync(csvfile.FullName, 0);
                            if (spd.Count > 0 && time.Count > 0)
                            {
                                if (!spd.Any(a => a == -1.0) && !time.Any(a => a == -1.0))
                                {
                                    var dis = await MileageCalculation.ReturnSingleDistance(spd, time);
                                    caldis += dis.Sum();
                                }
                            }
                        }
                        data.calmileage = Math.Round(caldis / 1000, 2);
                        data.difference = Math.Round(data.calmileage - data.odo, 1);
                        if (Math.Abs(data.difference) > 10)
                        {
                            data.info = $"{dirname}误差过大";
                        }
                        else
                        {
                            data.info = $"{dirname}里程正常";
                        }
                    }
                    else
                    {
                        data.info = $"{dirname}下没有数据{Environment.NewLine}";
                    }
                    result.Add(data);
                }
            }
            else
            {
                MileageCalculationResultModel data = new MileageCalculationResultModel();
                var noinfo = $"你选的日期路径没有数据{Environment.NewLine}";
                result.Add(data);
            }
            return result;
        }
    }
}
